package com.augus.admin.controller;

import com.augus.admin.bean.TUserRole;
import com.augus.admin.bean.Tuser;
import com.augus.admin.bean.User;
import com.augus.admin.service.TUserRoleService;
import com.augus.admin.service.TuserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Slf4j
@Controller
public class IndexController {

  @Autowired
  JdbcTemplate jdbcTemplate;

  @Autowired
  TuserService tuserService;

  @Autowired
  TUserRoleService tUserRoleService;

  //  @Autowired
  StringRedisTemplate redisTemplate;

  @ResponseBody
  @PostMapping("/role")
  public TUserRole saveCity(TUserRole tUserRole) {
    tUserRoleService.saveTUserRole(tUserRole);
    return tUserRole;
  }

  @ResponseBody
  @GetMapping("/role")
  public TUserRole getTuserRole(@RequestParam("id") Integer id) {
    return tUserRoleService.getById(id);
  }

  @ResponseBody
  @GetMapping("/tuser")
  public Tuser getById(@RequestParam("id") Integer id) {

    return tuserService.getUserid(id);
  }

  // 查看监控数据
  @ResponseBody
  @GetMapping("/sql")
  public String queryFromOb() {
    Long aLong = jdbcTemplate.queryForObject("select count(*) from t_user", Long.class);
    return aLong.toString();
  }

  /**
   * 来登录页面
   *
   * @return
   */
  @GetMapping(value = {"/", "/login"})
  public String loginPage() {

    return "login";
  }

  /**
   * 登录请求
   */
  @PostMapping("/login")
  public String main(User user, HttpSession session, Model model) {
    if (StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())) {
      // 把登录成功的用户存起来
      session.setAttribute("loginUser", user);
      // 重定向到redirect:/main.html 防止表单重复提交,
      return "redirect:/main.html";
    } else {
      // 回到登录页面
      model.addAttribute("msg", "账号密码错误");
      return "login";
    }
  }

  /**
   * 去main页面
   * 解决表单重复提交 -> redirect:/main.html
   */
  @GetMapping("/main.html")
  public String mainPage(HttpSession session, Model model) {
    // 做了拦截器了这个可以注销
    /*// 是否登录 拦截器,过滤器
    Object loginUser = session.getAttribute("loginUser");
    if(loginUser != null){
      return "main";
    }else{
      model.addAttribute("msg","请重新登录");
      return "login";
    }*/

    log.info("当前方法是:{}", "mainPage");

    ValueOperations<String, String> operations = redisTemplate.opsForValue();

    String s = operations.get("/main.html");
    String s1 = operations.get("/sql");

    model.addAttribute("mainCount", s);
    model.addAttribute("sqlCount", s1);

    return "main";
  }
}
